package com.homelink.android.explorerup.adapter;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;

import com.homelink.android.explorerup.R;

public class PathAbsAdapter extends BaseAdapter {
	String mAbsPath;
	Context mContext;
	int mGalleryItemBackground;
	ArrayList<String> mPaths;
	public static final String TAG = "PathAbsAdapter";

	// private int mSelectedIndex = 0;

	public PathAbsAdapter(Context context) {
		mContext = context;
		TypedArray attr = mContext.obtainStyledAttributes(R.styleable.path);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.path_android_galleryItemBackground, 0);
	}

	public void setAbsPath(String path) {
		mAbsPath = path;
		StringBuilder sb = new StringBuilder(path);
		if (mPaths == null) {
			mPaths = new ArrayList<String>(15);
		} else {
			mPaths.clear();
		}
		mPaths.add(">");
		int startIndex = 0;
		for (int i = 0; i < sb.length(); i++) {
			if (sb.charAt(i) == File.separatorChar) {

				mPaths.add(sb.substring(startIndex, i));
				mPaths.add(">");
				startIndex = i + 1;
			}
		}
		sb = null;

		System.gc();

	}

	public int getCount() {
		if (mPaths != null) {
			return mPaths.size();
		}
		return 0;
	}

	public void notifyDataSetChanged(int selected) {
		// mSelectedIndex = selected;
		super.notifyDataSetChanged();
	}

	public Object getItem(int position) {
		return mPaths.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TextView tv = new TextView(mContext);
		// if(mSelectedIndex == position){
		// tv.setTextSize(38);
		// }else{
		tv.setTextSize(30);
		// }
		tv.setText(mPaths.get(position));
		tv.setLayoutParams(new Gallery.LayoutParams(
				Gallery.LayoutParams.WRAP_CONTENT,
				Gallery.LayoutParams.FILL_PARENT));

		// tv.setBackgroundResource(mGalleryItemBackground);
		// Log.d(TAG, mPaths[position]);

		// return tv;
		// ImageView imageView = new ImageView(mContext);
		// imageView.setPadding(0, 2, 0, 2);
		// imageView.setLayoutParams(new
		// Gallery.LayoutParams(Gallery.LayoutParams.WRAP_CONTENT,80));
		// if(mSelectedIndex == position){
		// imageView.setImageBitmap(BitmapUtil.createReflectedImage(BitmapUtil.createTxtImage(mPaths[position],
		// 38,-1)));
		// imageView.setImageBitmap(BitmapUtil.createTxtImage(mPaths.get(position),
		// 38,-1));
		// }else{
		// imageView.setImageBitmap(BitmapUtil.createTxtImage(mPaths.get(position),
		// 30,-1));
		// }
		// imageView.setBackgroundResource(R.drawable.dir_bg);
		// imageView.setBackgroundResource(mGalleryItemBackground);
		return tv;
	}

}
